package info.fastpace.android.utils.list;

import info.fastpace.android.utils.Activity;
import info.fastpace.android.utils.list.ListDataAdapter.Binder;

import java.util.List;

import android.R.layout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ListViewBuilder<T> {

	private ListView listView;
	private ListDataAdapter<T> listDataAdapter;
//	private Activity activity;
//	private List<T> dataList = new ArrayList<T>();

	public ListViewBuilder() {
		super();
	}
	

	public void bind(Activity activity, ListView listView) {
		this.listView = listView;
//		this.activity = activity;
		
		listView.setFastScrollEnabled(true);
		listView.setSmoothScrollbarEnabled(true);

		listDataAdapter = new ListDataAdapter<T>(activity, getListItemResourceId());
		listDataAdapter.setBinder(createBinder());

//		listView.setAdapter(listDataAdapter);

		final OnItemClickListener itemClickListener = createItemClickListener();
		if (itemClickListener != null) {
			listView.setOnItemClickListener(itemClickListener);
		}

		final ItemLongClickListener<T> itemLongClickListener = createItemLongClickListener();
		if (itemLongClickListener != null) {
			listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
				@Override
				public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
					T item = listDataAdapter.getItem(position);
					itemLongClickListener.onItemLongClick(item);
					return true;
				}
			});
		}
		listView.setAdapter(listDataAdapter);
//		listDataAdapter.setItems(dataList);
	}

	public ListDataAdapter<T> getListDataAdapter() {
		return listDataAdapter;
	}

	public ListView getView() {
		return listView;
	}
	
	public List<T> getDataList() {
		return getListDataAdapter().getItems();
	}

	protected int getListItemResourceId() {
		return layout.simple_list_item_1;
	}

	protected Binder<T> createBinder() {
		return new ListDataAdapter.Binder.Util.BinderEmpty<T>();
	}
	
	protected OnItemClickListener createItemClickListener() {
		return null;
	}

	protected ItemLongClickListener<T> createItemLongClickListener() {
		return null;
	}
}
